var thumbanner = new Swiper('.thumbanner', {
	allowTouchMove: false,
	slidesPerView: 5,
	spaceBetween: 0,
	slidesPerGroup: 1,
	navigation: {
		nextEl: '.swiper-button-next',
		prevEl: '.swiper-button-prev',
	},

});
var bigbanner = new Swiper('.bigbanner', {
	effect: 'fade',
	fadeEffect: {
		crossFade: true,
	},
	allowTouchMove: false,
	thumbs: {
		swiper: thumbanner,
	},
});
// 商品放大镜
$('.bigbanner .banner').each(function(index, item) {
	$(this).mouseover(function() {
		$(this).find('.cover').css('display', 'block');
		$(this).find('.glass').css('display', 'block')
		var _src = $(this).find('img').attr('src');
		$(this).find('.glass').css('background-image', "url(" + _src + ")");
	})
	$(this).mouseleave(function() {
		$(this).find('.cover').css('display', 'none');
		$(this).find('.glass').css('display', 'none')

	})
	$(this).mousemove(function(e) {
		var bannerHeight = parseInt($(this).height());
		var bannerWidth = parseInt($(this).width());
		var coverHeight = parseInt($(this).find('.cover').height());
		var coverWidth = parseInt($(this).find('.cover').width());
		// cover div距离参照物的高度
		var _top = e.pageY - $('.bigbanner').offset().top - (coverHeight / 2);
		// 使cover竖直方向一直在box里面
		if (_top < 0) {
			_top = 0;
		} else if (_top > bannerHeight - coverHeight) {
			_top = bannerHeight - coverHeight;
		}

		var _left = e.pageX - $('.bigbanner').offset().left - (coverWidth / 2);
		// 使cover水平方向一直在box里面
		if (_left < 0) {
			_left = 0;
		} else if (_left > bannerWidth - coverWidth) {
			_left = bannerWidth - coverWidth;
		}
		$(this).find('.cover').css({
			left: _left + 'px'
		})
		$(this).find('.cover').css({
			top: _top + 'px'
		})
		$(this).find('.glass').css({
			'background-position': "-" + _left * 2 + "px -" + _top * 2 + "px"
		})

	})

})
// 颜色选择部分
// 颜色文本变更部分
$('.goodsdetail-r-color-choose-detail ul li').click(function() {
	var color = $(this).find('span').text();
	$(this).find('a').addClass('color-choose');
	$(this).siblings().find('a').removeClass('color-choose');
	$('.goodsdetail-r-haschoose-color').text(color);
})

// 数量增减部分
$('#goodsdetail-r-operation-stock-plus').click(function() {
	var num = parseInt($('.goodsdetail-r-operation-stock-text').val());

	var a = ++num;
	$('.goodsdetail-r-operation-stock-text').val(a);
	$('#goodsdetail-r-operation-stock-minus').css({
		color: ' #777',
		cursor: 'pointer'
	})
})
$('#goodsdetail-r-operation-stock-minus').click(function() {
	var num = parseInt($('.goodsdetail-r-operation-stock-text').val());
	if (num == 1) {
		$('.goodsdetail-r-operation-stock-text').val(1);
		$(this).css({
			color: ' #c4c4c4',
			cursor: 'not-allowed'
		})

		return;
	}
	var a = --num;
	$('.goodsdetail-r-operation-stock-text').val(a);
})
// 加入购物车弹出层
$('.right-float-main-hungbar-content-common-cart span').text(0);
$('.goodsdetail-r-operation-addcart').click(function() {

	if ($('.goodsdetail-r-haschoose-color').text() == '白色') {
		layer.confirm('华为路由AX2 Pro（白色）Wi-Fi6路由 畅享4K影片 提供手游加速 儿童上网保护成功加入购物车!', {
			btn: ['再逛逛', '去结算'],
		})
	} else if ($('.goodsdetail-r-haschoose-color').text() == '黑色') {
		layer.confirm('华为路由AX2 Pro（黑色）Wi-Fi6路由 畅享4K影片 提供手游加速 儿童上网保护成功加入购物车!', {
			btn: ['再逛逛', '去结算'],
		})
	} else {
		layer.msg('请选择颜色!');
		return;
	}
	var num = parseInt($('.goodsdetail-r-operation-stock-text').val()) + parseInt($(
		'.right-float-main-hungbar-content-common-cart span').text())
	$('.right-float-main-hungbar-content-common-cart span').css("display", "block").text(num);
	// $('.goodsdetail-r-operation-stock-text').val(1);
})

	function setCookie(cname, cvalue, exdays) {
				var d = new Date();
				d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
				var expires = "expires=" + d.toGMTString();
				document.cookie = cname + "=" + cvalue + "; " + expires;
			}

			function getCookie(cname) {
				var name = cname + "=";
				var ca = document.cookie.split(';');
				for (var i = 0; i < ca.length; i++) {
					var c = ca[i].trim();
					var arrC = c.split("=");
					if (arrC[0] == cname) {
						return arrC[1];
					}
				}
				return "";
			}
			$('.goodsdetail-r-operation-addcart').click(function() {
				var photo = $('.swiper-wrapper').children().eq(1).find('img').attr('src');
				var name = $('.goodsdetail-r-title').text().trim();
				var price = $('.goodsdetail-r-price span span').text().trim();
				var id = $('.goodsdetail-r').data('id');
				var num=parseInt($('.goodsdetail-r-operation-stock input').val()) ;
				var goods = {
					id: id,
					name: name,
					photo: photo,
					price: price,
					num: num
				}
				console.log(num);
				//读取cookie,在原先购物车数据基础上,添加新数据
				var cart = getCookie('cart');
				if (cart) {
					var goodsList = JSON.parse(cart);
				} else {
					var goodsList = [];
				}
				//检查购物车是否含有此商品
				var isExist = false;
				goodsList.forEach(function(item) {
					if (item.id == id) {
						isExist = true;
						item.num+=num;
					}
				})
				if (!isExist) {
					goodsList.push(goods);
				}
				//添加cookie
				setCookie('cart', JSON.stringify(goodsList));
			
			
			})
